package com.ykw.parking.config;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.Model;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    //ShiroFilterFactorybean  第三步
    @Bean(name="shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(
            @Qualifier("SecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean Bean = new ShiroFilterFactoryBean();
        //关联DefaultWebSecurityManager
        Bean.setSecurityManager(defaultWebSecurityManager);
        Map<String, String> Map=new LinkedHashMap();
        Map.put("/main.html","authc");
        Map.put("/price","authc");
        Map.put("/about","authc");
        Map.put("/services","authc");
        Map.put("/contact","authc");
        Map.put("/projects","authc");
        Map.put("/selling","authc");
        Map.put("/order","authc");
        Map.put("/threeDParking","authc");
        Bean.setFilterChainDefinitionMap(Map);
        Bean.setLoginUrl("/shirotologin");
        return Bean;
    }
    //DefaultWebSecurityManager  第二步
    @Bean(name="SecurityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(
            @Qualifier("UserRealm") UserRealm userRealm){
        DefaultWebSecurityManager SecurityManager = new DefaultWebSecurityManager();
        //关联Realm类
        SecurityManager.setRealm(userRealm);
        return SecurityManager;
    }
    //创建Realm对象 ,需要自定义类Realm类  第一步
    @Bean(name="UserRealm")
    public UserRealm userRealm(){
        return new UserRealm();
    }
}
